package cn.ckx.coding.common.exception;

import lombok.Getter;

import java.util.Objects;

/**
 * 异常构建
 * @author kunxin.cai 
 */
@Getter
public class BusinessError {

    private Integer code;

    private String message;

    private Object data;


    private BusinessError(Integer code, String message) {
        this(code, message, null);
    }

    private BusinessError(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static BusinessError builder(Integer code) {
        return builder(code, "");
    }

    public static BusinessError builder(Integer code, String message) {
        return new BusinessError(code, message);
    }
    
    public BusinessError msgFormat(Object ... args){
         this.message = String.format(message,args); 
         return this;
    } 

    public BusinessException build() {
        return new BusinessException(this.code, this.message);
    }

    public BusinessException build(Exception e) {
        return new BusinessException(this.code, this.message, e);
    }

    public BusinessException build(String message) {
        return new BusinessException(this.code, message);
    }

    public BusinessException build(Object data) {
        return new BusinessException(this.code, this.message, data);
    }

    public boolean match(BusinessException e) {
        return Objects.equals(this.code, e.getCode());
    }
}
